import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
// import whoami from '@dfeidao/atom-web/msg/whoami';
import set from '@dfeidao/atom-web/local/set';
import javadsd from '@dfeidao/atom-web/msg/javadsd';
import nodejs from '@dfeidao/atom-web/msg/nodejs';

import render from '@dfeidao/atom-web/render/render';
import P01 from './P01';
import P02 from './P02';
import P03 from './P03';
interface B {
	docDataStore: {
		master: {
			dataArray: [{
				no: string
				formid: string;
				grid_1: {
					dataArray: [
						{
							no: string;
							formid: string;
							formname: string;
							action_name: string;
							action_keyword: string;
						}
					]
				}
			}]
		};
	};
}
export default async function a001(fd: IFeidaoAiBrowserComponent) {
	// 初始化
	const productid1 = fd.data.node.querySelector<HTMLSpanElement>('[data-feidao-id="user"]').getAttribute('prid');
	// const user = await whoami(get_user_info);
	// console.log(user);
	// await put('productid', productid);
	set(fd, 'productid', productid1);
	const res = await nodejs<{ dataArray: Array<{}> }>('level-inf/zj-000001/s001', { productid: productid1 });
	render(fd, res.dataArray, P02, 'P02', 'inner');
	let client_point_id = '';
	const select = fd.data.node.querySelector<HTMLSelectElement>('[select]');
	for (let i = 0; i < select.options.length; i++) {
		if (select.options[i].text === 'web') {
			client_point_id = select.options[i].value;
			select.options[i].selected = true;
			break;
		}
	}
	const res1 = await nodejs<{ dataArray: Array<{}>; dd: Array<{}> }>('level-inf/zj-000001/s002', { productid: productid1, client_point_id });
	render(fd, res1.dataArray, P01, 'P01', 'inner');
	const select1 = fd.data.node.querySelector<HTMLSelectElement>('[select]');
	const index = select1.options.selectedIndex;
	const client_type1 = select1.options[index].value;
	const condition = {
		param: {
			productid: productid1,
			client_type: client_type1
		}
	};
	const ret = await javadsd('12', '12', 'smart_securitydsd0000001', condition) as B;
	const array = new Array();
	ret.docDataStore.master.dataArray.forEach((i, idex) => {
		if (i.grid_1.dataArray.length > 0) {
			i.grid_1.dataArray.forEach((j) => {
				j.no = j.formid;
				j.formid = j.formid + j.action_keyword;
				j.formname = j.action_name;
				array.push(j);
			});
		}
		delete i.grid_1;
		i.no = '';
		if (!i.formid) {
			i.formid = JSON.stringify(idex);
		}
		array.push(i);
	});
	render(fd, JSON.stringify(array), P03, 'P03', 'inner');
}
